<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--      &lt;!&ndash;部门数据&ndash;&gt;
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  default-expand-all
                  @node-click="handleNodeClick"
                />
              </div>
            </el-col>-->
      <table>
        <tr>
          <td><span>活动主题类别 :</span></td>
          <td><el-input v-model="form.subject_type" placeholder="请输入活动主题类别" style="width: 240px"></el-input></td>

        </tr>
        <br>
        <tr>
          <td>活动类别:</td>
          <td><el-input v-model="form.type" placeholder="请输入活动类别" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <tr>
          <td>活动标题:</td>
          <td><el-input v-model="form.title" placeholder="请输入活动标题" style="width: 240px"></el-input></td>
        </tr>
        <br>

        <tr>
          <td>活动地点:</td>
          <td><el-input v-model="form.position" placeholder="请输入活动地点" style="width: 240px"></el-input></td>
        </tr>
        <br>

        <tr>
          <td>活动要求:</td>
          <td><el-input v-model="form.activity_require" placeholder="请输入活动要求" style="width: 240px"></el-input></td>
        </tr>
        <br>

        <tr>
          <td>活动时间:</td>
          <td>
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd hh:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </td>
        </tr>
        <br>

        <tr>
          <td>文档附件:</td>
          <td><el-upload class="avatar-uploader"
                         :action="''"
                         :auto-upload="false"
                         :show-file-list="false"
                         :on-change="handleAccessoryChangeIcon">
            <img v-if="form.accessory" :src="form.accessory" class="avatar" style="height: 150px;">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload></td>
        </tr>
        <br>
        <br>
        <tr>
          <td><span>活动内容:</span></td>
          <td>
            <editor style="position: relative;z-index: 0;" ref="editorOne" v-model="form.content" ></editor>
          </td>

        </tr>
        <br>
        <tr>
          <td>活动截止时间:</td>
          <td>
            <el-date-picker
              v-model="form.max_sign_up_time"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </td>
        </tr>
        <br>

        <tr>
          <td>报名人数:</td>
          <td><el-input v-model="form.sign_up_people_count" placeholder="请输入报名人数" style="width: 240px" type="number"></el-input></td>
        </tr>
        <br>
        <tr>
          <td>附件：</td>
          <td><el-upload
            class="upload-demo"
            :headers="headers"
            ref="upload"
            :on-success="success"
            :action="uploadFile.url"
            :on-preview="handlePreview"
            :on-remove="handleRemoveFile"
            :file-list="fileList"
            :auto-upload="true">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <!--<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>-->
            <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
          </el-upload></td>
        </tr>
        <br>
        <tr>
          <td>备注:</td>
          <td><el-input v-model="form.remarks" placeholder="请输入备注" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <tr>
          <td></td>
          <td>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button type="info" @click="reset" style="margin-left: 30px;">重置</el-button></td>
        </tr>
      </table>

    </el-row>
  </div>
</template>
<script>
  import { listFile , getFile , addFile , updateFile ,delFile ,uploadFileImg} from "@/api/monitor/file";
  import { listActivity ,addActivity ,updateActivity ,getActivity} from "@/api/monitor/activity";
  import { listChannel , getChannel , addChannel , updateChannel ,delChannel,getParentChannel} from "@/api/monitor/channel";
  import { getToken } from "@/utils/auth";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Editor from '@/components/wangEditor'

  export default {
    components: { Treeselect  ,Editor },
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '明天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周后',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        isClear: false,
        detail:"",
        user: {},
        sexs: [],
        option:[],
        id_card_type: [],
        status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        Options: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,

        },
        fileList:[],
        upload: {
          url: process.env.VUE_APP_BASE_API + "/uploadFileImg",

        },
        uploadFile: {
          url: process.env.VUE_APP_BASE_API + "/uploadAccessoryFile",

        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          name: undefined,
          status: undefined
        },
        /*        // 表单校验
                rules: {
                  userName: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" }
                  ],
                  nickName: [
                    { required: true, message: "用户昵称不能为空", trigger: "blur" }
                  ],

                  email: [
                    {
                      type: "email",
                      message: "'请输入正确的邮箱地址",
                      trigger: ["blur", "change"]
                    }
                  ],
                  phonenumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: "请输入正确的手机号码",
                      trigger: "blur"
                    }
                  ]
                }*/
      };
    },
    computed:{
      headers(){
        return{
          token: getToken()
        }
      }
    },
    created() {
      const activityId = this.$route.params && this.$route.params.activityId;
      this.getActivity(activityId);
    },

    methods: {
      success(res){  //上传文件成功的回调函数
        console.log(res)
        this.form.accessory_file =this.form.accessory_file +  res.data[0].url +",";
        console.log(this.form.accessory_file)
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemoveFile(file, fileList) {
        var name = file.name;
        var strings = this.form.accessory_file.split(",")
        for (let i = 0; i <strings.length; i++) {
          if (strings[i]==name){
            strings.splice(i,1);
          }
        }

        var s = strings.toString()
        this.form.accessory_file = s;

        console.log(this.form.accessory_file);
      },
      handlePreview(file) {
        console.log(file);
      },
      submit () {
        if (this.dateRange!=null){
          this.form.activity_time = this.dateRange;
        }
        updateActivity(this.form).then(rs =>{
          this.msgSuccess("修改成功");
        })
      },
      // 表单重置
      reset() {
        this.form = {
          subject_type: undefined,
          type: undefined,
          title: undefined,
          activity_time: undefined,
          position: undefined,
          content: undefined,
          activity_require: undefined,
          max_sign_up_time: undefined,
          sign_up_people_count: undefined,
          remarks: undefined,
        };
        this.resetForm("form");
      },
      //获取频道
      getListChannle(){
        listChannel(this.addDateRange(this.queryParams, this.dateRange)).then(rs =>{
          this.option = rs.data.items;
          console.log(this.option)
        })
      },
      getActivity(activityId){
        getActivity(activityId).then(rs =>{
          this.form = rs.data;
          let time = rs.data.activity_time.substring(1,rs.data.activity_time.length-1);
          let strings = time.split(",")
          this.dateRange =[strings[0],strings[1]]
          var accessoryFile = rs.data.accessory_file;
          if (accessoryFile!=null && accessoryFile!=undefined && accessoryFile!=""){
            var list = accessoryFile.split(",");
            for (let i = 0; i < list.length; i++) {
              if (list[i]!=null && list[i] != "" && list[i]!=undefined){
                this.fileList.push({name:list[i],url:list[i]})
              }
            }

          }
          console.log(this.dateRange)
        })
      },
      /*文档附件上传*/
      handleAccessoryChangeIcon(file, fileList) {//选中文件触发的change事件
        console.log(file)
        const isJPG = file.raw.type === 'image/jpeg'
        const isPNG = file.raw.type === 'image/png'
        // const isLt2M = file.raw.size / 1024 / 1024 < 0.5  //限制上传文件的大小
        if (!isPNG && !isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
          return false
        } else {
          this.form.accessory = URL.createObjectURL(file.raw);//赋值图片的url，用于图片回显功能
          this.uploadImg(file)//调用上传文件api接口
          this.$forceUpdate()
        }
      },
      uploadImg(file){//文件上传操作
        let formData=new FormData()
        formData.append('file',file.raw)
        uploadFileImg(formData).then(res=>{
          console.log(res)
          if (res.code == '20000') {
            this.msgSuccess(res.data.msg);
            this.form.accessory = res.data[0].url;
            console.log(this.form.accessory)
          }
        })
      },

    }

  };
</script>
<style>
  .myQuillEditor{
    height: 300px;
  }
</style>
